<script setup lang="ts">
import { defineProps } from 'vue'

const props = defineProps<{
  app: {
    id?: string
    appName?: string
    cover?: string
    deployKey?: string
    user?: {
      userName?: string
      userAvatar?: string
    }
  }
  isFeatured?: boolean
  defaultAuthorName?: string
}>()

const emit = defineEmits<{
  viewApp: [appId: string]
  viewDeployedApp: [deployKey: string]
}>()

const handleViewApp = () => {
  if (props.app.id) {
    emit('viewApp', props.app.id)
  }
}

const handleViewDeployedApp = () => {
  if (props.app.deployKey) {
    emit('viewDeployedApp', props.app.deployKey)
  }
}
</script>

<template>
  <div :class="['app-card', { 'featured-card': isFeatured }]">
    <div :class="['app-preview', { 'featured-preview': isFeatured }]">
      <img v-if="app.cover" :src="app.cover" :alt="app.appName" />
      <div v-else :class="['app-placeholder', { 'featured-placeholder': isFeatured }]">
        <span>🤖</span>
      </div>
      <div :class="['app-overlay', { 'featured-overlay': isFeatured }]">
        <a-button type="primary" @click.stop="handleViewApp">查看对话</a-button>
        <a-button v-if="app.deployKey" type="default" @click.stop="handleViewDeployedApp">查看作品</a-button>
      </div>
    </div>
    <div :class="['app-info', { 'featured-info': isFeatured }]">
      <div class="info-content">
        <a-avatar :src="app.user?.userAvatar || ''" size="small" class="user-avatar" />
        <div class="text-content">
          <h3 :class="['app-title', { 'featured-title': isFeatured }]">{{ app.appName || '未命名应用' }}</h3>
          <p :class="['app-author', { 'featured-author': isFeatured }]">
            {{ app.user?.userName || (defaultAuthorName || '未知用户') }}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.app-card,
.featured-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.app-card:hover,
.featured-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.app-preview {
  height: 180px;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.featured-preview {
  height: 200px;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.app-overlay,
.featured-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s;
  gap: 12px;
}

.app-card:hover .app-overlay,
.featured-card:hover .featured-overlay {
  opacity: 1;
}

.app-preview img,
.featured-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.app-placeholder,
.featured-placeholder {
  font-size: 48px;
  color: #d9d9d9;
}

.app-info,
.featured-info {
  padding: 16px;
}

.info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  flex-shrink: 0;
}

.text-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.app-title,
.featured-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-author,
.featured-author {
  font-size: 14px;
  color: #8c8c8c;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>